package com.magpiebridge.controller;


import com.magpiebridge.pojo.User;
import com.magpiebridge.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/UserAll")
    public List<User> getAllUser(){
        return userService.getAllUser();
    }

    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Integer id) {
        return userService.getUserById(id);
    }


    @PostMapping("/insertUser")
    public String insertUser(@RequestBody User user){
        Integer userId = user.getId();
        String userName = user.getName();
        String userHeadPhoto = user.getHeadPhoto();
        Boolean userState = user.getState();
        String userIdCard = user.getIdCard();
        Integer userPhone = user.getPhone();
        Boolean userVip = user.getVip();
        Integer userMoney = user.getUserMoney();
        Date createTime = user.getCreateTime();

        int result = userService.insertUser(user);
        if (result > 0){
            return "用户添加成功";
        } else {
            return "用户添加失败";
        }
    }

    @DeleteMapping("/deleteUser/{id}")
    public  String deleteUser(@PathVariable  Integer  id)  {
        try  {
            //  查询用户是否存在
            User  user  =  userService.getUserById(id);
            if  (user  ==  null)  {
                return  "用户不存在";
            }
            //  删除用户
            int  result  =  userService.deleteUser(id);
            if  (result  >  0)  {
                //  更新数据库状态
                return  "用户删除成功";
            }  else  {
                return  "用户删除失败";
            }
        }  catch  (Exception  e)  {
            //  记录日志或警告
            //  TODO:  Log  deletion  information
            // 打印错误信息
            System.err.println(e.getMessage());
            return "系统错误";
        }
    }

    @PostMapping("/updateUser/{id}")
    public String updateUser( @RequestBody User newUser) {
        try {
            // 查询用户是否存在
            User user = userService.getUserById(newUser.getId());
            if (user == null) {
                return "用户不存在";
            }else{
                // 更新用户信息
                user.setId(newUser.getId());
                user.setName(newUser.getName());
                user.setHeadPhoto(newUser.getHeadPhoto());
                user.setState(newUser.getState());
                user.setIdCard(newUser.getIdCard());
                user.setPhone(newUser.getPhone());
                user.setVip(newUser.getVip());
                user.setUserMoney(user.getUserMoney());
                user.setCreateTime(newUser.getCreateTime());

                int result = userService.updateUser(user);
                if (result > 0) {
                    return "用户更新成功";
                } else {
                    return "用户更新失败";
                }
            }

        } catch (Exception e) {
            // 记录日志或警告
            // TODO: Log update information
            // 打印错误信息
            System.err.println(e.getMessage());
            return "系统错误";
        }
    }

    @PutMapping("/updateUserState/{id}")
    public String updateUserState(@PathVariable Integer id) {
        boolean success = userService.updateUserState(id);
        if (success) {
            return "用户修改状态成功";
        } else {
            return "没有找到该用户";
        }
    }
}
